package com.aiss.ws.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.aiss.ws.Entity.AnswerItem;
import com.aiss.ws.Entity.ExamItem;
import com.aiss.ws.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenqiang on 15/12/29.
 */
public class AnswerAdatper extends BaseAdapter{

    private List<AnswerItem> answerItems;
    private Context context;
    private ExamItem item;
    private int positionItem;

    public AnswerAdatper(List<AnswerItem> answerItems, ExamItem item, int positionItem, Context context) {
        this.answerItems = answerItems;
        this.context = context;
        this.item = item;
        this.positionItem = positionItem;
    }

    @Override
    public int getCount() {
        return answerItems.size();
    }

    @Override
    public Object getItem(int position) {
        return answerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView==null){
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.answer_item,null);
            holder.title_tv = (TextView) convertView.findViewById(R.id.title_tv);
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.linearlayout = (LinearLayout) convertView.findViewById(R.id.linearlayout);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }

        holder.title_tv.setText(answerItems.get(position).getKey()+"、"+answerItems.get(position).getValue());

        if(item.getTestItems().get(positionItem).getType().equals("单选题")||item.getTestItems().get(positionItem).getType().equals("判断题")){
            if(item.getTestItems().get(positionItem).getMe_answer()!=null){
                if(item.getTestItems().get(positionItem).getMe_answer().equals(""+position)){
                    holder.img.setImageResource(R.mipmap.iocn_2x12);
                }else{
                    holder.img.setImageResource(R.mipmap.iocn_2x13);
                }


            }else{
                holder.img.setImageResource(R.mipmap.iocn_2x13);
            }

            holder.linearlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    item.getTestItems().get(positionItem).setMe_answer(""+position);
                    notifyDataSetChanged();

                }
            });
        }else{//当前为多选题

            if(item.getTestItems().get(positionItem).getMe_more_answer()!=null){

                List<String> list = item.getTestItems().get(positionItem).getMe_more_answer();

                if(list.contains(position+"")){
                    holder.img.setImageResource(R.mipmap.iocn_2x12);
                }else{
                    holder.img.setImageResource(R.mipmap.iocn_2x13);
                }

//                for(int i = 0 ; i < list.size() ;i++){
//                    if(list.get(i)==position){
//                        holder.img.setImageResource(R.mipmap.ico1);
//                    }else{
//                        holder.img.setImageResource(R.mipmap.ico0);
//                    }
//                }


            }else{
                holder.img.setImageResource(R.mipmap.iocn_2x13);
            }

            holder.linearlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(item.getTestItems().get(positionItem).getMe_more_answer()==null){
                        item.getTestItems().get(positionItem).setMe_more_answer(new ArrayList<String>());
                    }

                    List<String> set = item.getTestItems().get(positionItem).getMe_more_answer();

                    if(set.contains(position+"")){
                        set.remove(position+"");
                    }else{
                        set.add(position+"");
                    }

                    /**
                     * 冒泡排序
                     */
                    for(int i = 0 ; i < set.size()-1;i++){

                        for(int j = i+1 ; j < set.size();j++){

                            if(set.get(i).compareTo(set.get(j))>=0){
                                String k = set.get(i);
                                set.set(i,set.get(j));
                                set.set(j,k);
                            }
                        }
                    }
                    item.getTestItems().get(positionItem).setMe_more_answer(set);
                    notifyDataSetChanged();

                }
            });

        }



        return convertView;
    }

    class Holder{
        LinearLayout linearlayout;
        ImageView img;
        TextView title_tv;
    }

}
